#include "DxLib.h"
#include "Map.h"
#include "../Maths/Point.h"
#include "../Game/Camera.h"

Map::Map(int width, int height){
	Init(width, height);
}

Map::~Map(){
}

void Map::Init(int width, int height){
	this->width = width;
	this->height = height;
	floors = vector< vector<shared_ptr<Floor> > >(width, vector<shared_ptr<Floor> >(height));
	for(int i=0; i<width; i++){
		for (int j=0; j<height; j++){
			floors[i][j] = std::make_shared<Floor>(WALL);
		}
	}

	tipSize = 32;
	floorGrapicHandle = LoadGraph("Resources\\Images\\floor.bmp");
}

shared_ptr<Floor> Map::GetFloor(int x, int y){
	return floors[x][y];
}

void Map::SetFloor(int x,int y, FloorType type){
	floors[x][y]->SetFloorType(type);
}

bool Map::IsMovable(const Point& point){
	shared_ptr<Floor> floor = GetFloor(point.X,point.Y);
	FloorType type = floor->GetFloorType();
	if (type == FLOOR){
		return true;
	}
	if (type == WALL){
		return false;
	}}

FloorType Map::GetFloorType(const Point& point){
	shared_ptr<Floor> floor = GetFloor(point.X,point.Y);
	return floor->GetFloorType();
}

Vector2 Map::GetPosFromIndex( const Point& point )
{
	if ( point.X < 0 
		|| point.Y < 0
		|| point.X >= width
		|| point.Y >= height
		)
	{
		return Vector2();
	}

	return Vector2( static_cast<float>( tipSize * point.X ),
					static_cast<float>( tipSize * point.Y ) 
					);
}

void Map::Update( float delta ){
}

void Map::Draw(){
	for(int i=0; i<width; i++){
		for (int j=0; j<height; j++){
			FloorType type = floors[i][j]->GetFloorType();

			int srcX = 0;
			int srcY = 0;
			if (type == FLOOR){
				srcX = 0;
				srcY = 0;
			}else if(type == WALL){
				srcX = 32;
				srcY = 0;
			}
				
			DrawRectGraph(
				tipSize * i - Camera::GetInstance().GetX(), 
				tipSize * j - Camera::GetInstance().GetY(),
				srcX,
				srcY,
				tipSize,
				tipSize,
				floorGrapicHandle,
				TRUE,
				FALSE
			);
			
		}
	}
}